package com.jerry.usbserial.activity

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.jerry.usbserial.R
import com.jerry.usbserial.fragment.udp.NormalConfigFragment
import com.jerry.usbserial.fragment.udp.SmartConnectFragment
import com.jerry.usbserial.fragment.udp.UdpMenuFragment
import com.jerry.usbserial.fragment.usb.UsbFirmwareFragment
import com.jerry.usbserial.fragment.usb.UsbMenuFragment
import kotlinx.android.synthetic.main.act_main.*
import kotlinx.android.synthetic.main.bar_common.*
import kotlinx.android.synthetic.main.dlg_page_select.view.*

class MainActivity : AppCompatActivity() {
    private val mContext: Context by lazy { this }

    private var clickTimes = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        setContentView(R.layout.act_main)
        initView()
    }

    @SuppressLint("InflateParams", "SetTextI18n")
    private fun initView() {
        val verName = packageManager.getPackageInfo(packageName, 0).versionName.split("_")[0]
        toolbar_title.text = "UHC固件升级工具(v$verName)"

        showDialog()
    }

    override fun onBackPressed() {
        showDialog()
    }

    @SuppressLint("InflateParams")
    private fun showDialog() {
        val dialog = Dialog(mContext, R.style.Dialog_Default)
        val view = layoutInflater.inflate(R.layout.dlg_page_select, null)
        dialog.setContentView(view)
        view.model_selected.setOnClickListener {
            clickTimes++
            if (clickTimes == 5) {
                view.bt_normal_config.visibility = View.VISIBLE
                view.bt_one_click.visibility = View.VISIBLE
            }
            if (clickTimes == 7) {
                view.bt_firmware.performClick()
            }
        }
        view.bt_firmware.setOnClickListener {
            supportFragmentManager.beginTransaction().apply {
                replace(R.id.frame_container, UsbFirmwareFragment())
                commit()
            }
            appBar.visibility = View.GONE
            dialog.dismiss()
        }
        view.bt_menu.setOnClickListener {
            supportFragmentManager.beginTransaction().apply {
                replace(R.id.frame_container, UsbMenuFragment())
                commit()
            }
            appBar.visibility = View.GONE
            dialog.dismiss()
        }
        view.bt_menu_udp.setOnClickListener {
            supportFragmentManager.beginTransaction().apply {
                replace(R.id.frame_container, UdpMenuFragment())
                commit()
            }
            appBar.visibility = View.GONE
            dialog.dismiss()
        }
        view.bt_one_click.setOnClickListener {
            supportFragmentManager.beginTransaction().apply {
                replace(R.id.frame_container, SmartConnectFragment())
                commit()
            }
            appBar.visibility = View.GONE
            dialog.dismiss()
        }
        view.bt_normal_config.setOnClickListener {
            supportFragmentManager.beginTransaction().apply {
                replace(R.id.frame_container, NormalConfigFragment())
                commit()
            }
            appBar.visibility = View.GONE
            dialog.dismiss()
        }

        dialog.setCancelable(false)
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
        dialog.window?.attributes = dialog.window?.attributes?.apply {
            width = mContext.resources.displayMetrics.widthPixels * 8 / 10
        }
    }
}